#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define S1(i) s1[(i)-1]
#define S2(i) s2[(i)-1]
#define S(i,j) s[(i)*(m+1)+j]
#define C(i,j) c[(i)*(m+1)+j]
int lcsprint(char *s2,int m,char *s,int i,int j,char *p,int temp)
{
	if(i==0||j==0)
	{
		printf("%s\n",p);		
		return 0;
	}
	if(S(i,j)==92) 
	{
		p[temp-1]=S2(j);
		lcsprint(s2,m,s,i-1,j-1,p,temp-1);
		return 0;
	}
	if(S(i,j)==43||S(i,j)==124) lcsprint(s2,m,s,i-1,j,p,temp);
	if(S(i,j)==43||S(i,j)==45) lcsprint(s2,m,s,i,j-1,p,temp);
	return 0;
}

int main()
{
	int i,j,k;
	char *s1,*s2;
	int n,m,temp;
	int *c;
	char *s,*p;
	scanf("%d %d",&n,&m);
	s1=(char *)malloc((n+1)*sizeof(char));
	s2=(char *)malloc((m+1)*sizeof(char));
	c=(int *)calloc((n+1)*(m+1),sizeof(int));
	s=(char *)calloc((n+1)*(m+1),sizeof(int));
	scanf("%s %s",s1,s2);
	for(i=0;i<=n;i++) C(i,0)=0;
	for(i=0;i<=m;i++) C(0,i)=0;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++) 
		{
			if(S1(i)==S2(j))
			{	
				C(i,j)=C(i-1,j-1)+1;
				S(i,j)=92;
			}
			else
			{
				if(C(i-1,j)>C(i,j-1))
				{
					C(i,j)=C(i-1,j);
					S(i,j)=124;
				}
				else
				{
					C(i,j)=C(i,j-1);
					S(i,j)=45;
				}
				if(C(i-1,j)==C(i,j-1)) S(i,j)=43;
			}
		}
	}
	for(i=0;i<=n;i++) 
	{
		for(j=0;j<=m;j++)
		{ 
			printf("%d ",C(i,j));
		}
		printf("\n");
	}
	
	for(i=0;i<=n;i++) 
	{
		for(j=0;j<=m;j++)
		{ 
			printf("%c ",S(i,j));
		}
		printf("\n");
	}

	temp=C(n,m);
	p=(char *)malloc((temp+1)*sizeof(char));
	lcsprint(s2,m,s,n,m,p,temp);
	free(s1);
	free(s2);
	free(s);
	free(p);
	free(c);
	return 0;
}
